package interview.blockqueue;

import java.util.concurrent.SynchronousQueue;

public class SynchronousQueueDemo {

    public static void main(String[] args) {

    /*    SynchronousQueue synchronousQueue = new SynchronousQueue();
            new Thread(() -> {
                try {
                    synchronousQueue.put(1);
                    System.out.println("==================1====================");
                    synchronousQueue.put(2);
                    System.out.println("==================2====================");
                    synchronousQueue.put(3);
                    System.out.println("==================3====================");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();


        new Thread(() -> {
            try {
                Thread.sleep(2000);
                synchronousQueue.take();
                Thread.sleep(2000);
                synchronousQueue.take();
                Thread.sleep(2000);
                synchronousQueue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();*/

        SynchronousQueue synchronousQueue = new SynchronousQueue();
        new Thread(() -> {
            try {
                synchronousQueue.put(1);
                System.out.println("==================1====================");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            try {
                synchronousQueue.put(2);
                System.out.println("==================2====================");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            try {
                synchronousQueue.put(3);
                System.out.println("==================3====================");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();



        new Thread(() -> {
            try {
                Thread.sleep(2000);
                synchronousQueue.take();
                synchronousQueue.take();
                synchronousQueue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();


    }
}
